<?php
include "mysql_config.php";

set_time_limit(0);
date_default_timezone_set("Asia/Shanghai");
header("Content-Type: text/html; charset=UTF-8");
$mysqlConfig = new MysqlConfig();
$mysqli = new mysqli($mysqlConfig->getMysqlServerName(), $mysqlConfig->getMysqlUserName(), $mysqlConfig->getMysqlPassword(), $mysqlConfig->getMysqlDatabase());
$yesterday = date('Y-m-d',strtotime("-1day"));
$startTime = $yesterday . " 00:00:00";
$lastTime = $yesterday . " 23:59:59";
$citySql = "select * from 91fbw_city where status=1";
$cityResult = $mysqli->query($citySql);
while ($row = mysqli_fetch_assoc($cityResult)) {
    //cityid,date,orderamount,servicefeeincome,redenvelopeamount,supermarketsubsidy,income
    $cityId = $row['id'];
    $isExist = 0;
    $isExistSql = "select count(*) as count from 91fbw_agent_profit_detail where date = '".$yesterday."' and cityid=".$cityId.";";
    $isExistResult = $mysqli->query($isExistSql);
    while ($isExistRow = mysqli_fetch_assoc($isExistResult)) {
        if($isExistRow['count']>0) {
            $isExist = 1;
        }
    }
    if($isExist) {
        continue;
    }
    $orderAmount = 0;
    $orderAmountSql = "select sum(orderamount) as sum from 91fbw_orders where cityid=". $cityId." and created_at>='".$startTime."' and created_at<='".$lastTime."'; ";
    $orderAmountResult = $mysqli->query($orderAmountSql);
    while ($orderAmountRow = mysqli_fetch_assoc($orderAmountResult)) {
        $orderAmount = $orderAmountRow['sum'];
    }
    $serviceFeeIncome = $orderAmount * 0.08 * 0.7 * 0.5;
    $serviceFeeIncome = sprintf("%.2f",substr($serviceFeeIncome,0,(stripos($serviceFeeIncome,'.')+3)));
    $redEnvelopeAmount = 0;
    $redEnvelopeAmountSql = "select sum(agentamount) as sum from 91fbw_orders where cityid=". $cityId." and created_at>='".$startTime."' and created_at<='".$lastTime."'; ";
    $redEnvelopeAmountResult = $mysqli->query($redEnvelopeAmountSql);
    while ($redEnvelopeAmountRow = mysqli_fetch_assoc($redEnvelopeAmountResult)) {
        $redEnvelopeAmount = $redEnvelopeAmountRow['sum'];
        $redEnvelopeAmount =  sprintf("%.2f",substr($redEnvelopeAmount/2,0,(stripos($redEnvelopeAmount/2,'.')+3)));
    }
    $supermarketSubsidy = 0;
//    $supermarketSubsidySql = "select sum(subsidy) as sum from 91fbw_supermarket_coupon where cityid=". $cityId." and usetime>='".$startTime."' and usetime<='".$lastTime."'; ";
//    $supermarketSubsidyResult = $mysqli->query($supermarketSubsidySql);
//    while ($supermarketSubsidyRow = mysqli_fetch_assoc($supermarketSubsidyResult)) {
//        $supermarketSubsidy = $supermarketSubsidyRow['sum'];
//    }
    $income = $serviceFeeIncome - $redEnvelopeAmount - $supermarketSubsidy;
    $insertSql = "insert into 91fbw_agent_profit_detail (`cityid`,`date`,`orderamount`,`servicefeeincome`,`redenvelopeamount`,`supermarketsubsidy`,`income`, `created_at`, `updated_at`)" .
         " values(".$cityId.",'".$yesterday."',".$orderAmount.",".$serviceFeeIncome.", ".$redEnvelopeAmount.",".$supermarketSubsidy.", ".$income.",'".date('Y-m-d H:i:s')."','".date('Y-m-d H:i:s')."');";
    $mysqli->query($insertSql);
}
$mysqli->close();